using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.Sql;
using System.Data.SqlClient;

namespace Database
{
   public class HangSX
    {
        private int mahangsx;

        public int Mahangsx
        {
            get { return mahangsx; }
            set { mahangsx = value; }
        }
        private string tenhang;

        public string Tenhang
        {
            get { return tenhang; }
            set { tenhang = value; }
        }
        private string quocgia;

        public string Quocgia
        {
            get { return quocgia; }
            set { quocgia = value; }
        }
        private string lienhe;

        public string Lienhe
        {
            get { return lienhe; }
            set { lienhe = value; }
        }
        private string logo;

        public string Logo
        {
            get { return logo; }
            set { logo = value; }
        }
       public HangSX()
       {
       }
       public HangSX(int mahangsx, string tenhang, string quocgia, string lienhe, string logo)
       {
           this.mahangsx = mahangsx;
           this.tenhang = tenhang;
           this.quocgia = quocgia;
           this.lienhe = lienhe;
           this.logo = logo;
       }
       public static DataSet DSHSX()
       {
           DataSet ds = new DataSet();
           string query = "select * from hangsx";
           sqlConnection db = new sqlConnection();
           SqlDataAdapter sda = new SqlDataAdapter(query,db.GetConnect());
           sda.Fill(ds);
           return ds;
       }
       public DataSet DSNhaSanXuat()
       {
           DataSet ds = new DataSet();
           string query = "select * from hangsx";
           sqlConnection db = new sqlConnection();
           SqlDataAdapter sda = new SqlDataAdapter(query, db.GetConnect());
           sda.Fill(ds);
           return ds;
       }
       public static DataSet DSMaHSX()
       {
           DataSet ds = new DataSet();
           string query = "select mahangsx,tenhang from hangsx";
           sqlConnection db = new sqlConnection();
           SqlDataAdapter sda = new SqlDataAdapter(query, db.GetConnect());
           sda.Fill(ds);
           return ds;
       }

       public bool ThemNSX(string tenhang, string quocgia, string lienhe, string logo)
       {
           bool kq = true;

           try
           {
               string query = "insert into HangSX(tenhang,quocgia,lienhe,logo) values('{0}','{1}','{2}','{3}')";
               query = String.Format(query,tenhang,quocgia,lienhe,logo);
               sqlConnection db = new sqlConnection();
               SqlCommand cmd = new SqlCommand(query,db.GetConnect());
              return cmd.ExecuteNonQuery()>0;
           }
           catch
           {
               kq = false;
           }

           return kq;
       }
       public bool XoaNSX(int mahangsx)
       {
           bool kq = true;

           try
           {
               string query = "Delete HangSX where mahangsx='{0}'";
               query = String.Format(query,mahangsx);
               sqlConnection db = new sqlConnection();
               SqlCommand cmd = new SqlCommand(query, db.GetConnect());
               cmd.ExecuteNonQuery();
           }
           catch
           {
               kq = false;
           }

           return kq;
       }
       public bool CapNhatNSX(int mahangsx,string tenhang, string quocgia, string lienhe, string logo)
       {
           bool kq = true;

           try
           {
               string query = "update HangSX set tenhang='{0}',quocgia='{1}',lienhe='{2}',logo='{3}' where mahangsx={4}";
               query = String.Format(query,tenhang,quocgia,lienhe,logo,mahangsx);
               sqlConnection db = new sqlConnection();
               SqlCommand cmd = new SqlCommand(query,db.GetConnect());
              return cmd.ExecuteNonQuery()>0;
           }
           catch
           {
               kq = false;
           }

           return kq;
       }
       public HangSX ThongTinHSX(int mahangsx)
       {
           string query = "select * from HangSX where mahangsx=" + mahangsx;
           sqlConnection db = new sqlConnection();
           SqlCommand sc = new SqlCommand(query, db.GetConnect());
           SqlDataReader reader = sc.ExecuteReader();
           HangSX hsx = new HangSX();
           while (reader.Read())
           {
               hsx.mahangsx = reader.GetInt32(0);
               hsx.tenhang = reader.GetString(1);
               hsx.quocgia = reader.GetString(2);
               hsx.lienhe = reader.GetString(3);
               hsx.logo = reader.GetString(4);               
           }
           return hsx;
       }
       public DataTable DanhSachHangSX()
       {
           DataTable ds = new DataTable();
           try
           {
               string query = "select * from hangsx";
               sqlConnection db = new sqlConnection();
               SqlDataAdapter sda = new SqlDataAdapter(query, db.GetConnect());
               sda.Fill(ds);
           }
           catch
           {
               ds = null;
           }
           return ds;
       }
       public DataTable DSHangSanXuatTheoMa(int mh)
       {
           DataSet ds = new DataSet();
           try
           {
               string query = "select * from hangsx where mahangsx={0}";
               query = string.Format(query, mh);
               sqlConnection db = new sqlConnection();
               SqlDataAdapter sda = new SqlDataAdapter(query, db.GetConnect());
               sda.Fill(ds);
           }
           catch
           {
               ds = null;
           }
           return ds.Tables[0];
       }

    }
}
